package com.camemax.rabbitmq.fanout_mode;

import com.camemax.rabbitmq.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/*
***************************
*   @Date: 2021-06-06
*   @Description: Publisher in RabbitMQ fanout mode
***************************
*/
public class FanoutMode_Publisher {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = RabbitMQUtils.createFactory();

        Channel channel = null;
        Connection connection = null;

        try {
            connection = connectionFactory.newConnection("fanout-publisher");
            channel = connection.createChannel();

            String exchangeName = "fanout-exchange";
            String routingKey = "";
            String message = "Publisher sends out message to Consumer in fanout mode by Java!";

            channel.basicPublish(
                    exchangeName,
                    routingKey,
                    null,
                    message.getBytes()
            );

            System.out.println("Publisher sends finished!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RabbitMQUtils.close(channel, connection);
        }

    }
}
